// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <benchmark/benchmark.h>

#include <nebula/core/array.h>
#include <nebula/core/datum.h>
#include <nebula/testing/ktest_util.h>
#include <nebula/testing/random.h>

#include <nebula/compute/api_vector.h>

namespace nebula {
namespace compute {


static constexpr random::SeedType kRandomSeed = 0xabcdef;
static constexpr random::SeedType kLongLength = 16384;

static std::shared_ptr<Array> MakeReplacements(random::RandomArrayGenerator* generator,
                                               const BooleanArray& mask) {
  int64_t count = 0;
  for (int64_t i = 0; i < mask.length(); i++) {
    count += mask.value(i) && mask.is_valid(i);
  }
  return generator->Int64(count, /*min=*/-65536, /*max=*/65536, /*null_probability=*/0.1);
}

static void ReplaceWithMaskLowSelectivityBench(
    benchmark::State& state) {  // NOLINT non-const reference
  random::RandomArrayGenerator generator(kRandomSeed);
  const int64_t len = state.range(0);
  const int64_t offset = state.range(1);

  auto values =
      generator.Int64(len, /*min=*/-65536, /*max=*/65536, /*null_probability=*/0.1)
          ->slice(offset);
  auto mask = turbo::checked_pointer_cast<BooleanArray>(
      generator.Boolean(len, /*true_probability=*/0.1, /*null_probability=*/0.1)
          ->slice(offset));
  auto replacements = MakeReplacements(&generator, *mask);

  for (auto _ : state) {
    ABORT_NOT_OK(ReplaceWithMask(values, mask, replacements));
  }
  state.SetBytesProcessed(state.iterations() * (len - offset) * 8);
}

static void ReplaceWithMaskHighSelectivityBench(
    benchmark::State& state) {  // NOLINT non-const reference
  random::RandomArrayGenerator generator(kRandomSeed);
  const int64_t len = state.range(0);
  const int64_t offset = state.range(1);

  auto values =
      generator.Int64(len, /*min=*/-65536, /*max=*/65536, /*null_probability=*/0.1)
          ->slice(offset);
  auto mask = turbo::checked_pointer_cast<BooleanArray>(
      generator.Boolean(len, /*true_probability=*/0.9, /*null_probability=*/0.1)
          ->slice(offset));
  auto replacements = MakeReplacements(&generator, *mask);

  for (auto _ : state) {
    ABORT_NOT_OK(ReplaceWithMask(values, mask, replacements));
  }
  state.SetBytesProcessed(state.iterations() * (len - offset) * 8);
}

BENCHMARK(ReplaceWithMaskLowSelectivityBench)->Args({kLongLength, 0});
BENCHMARK(ReplaceWithMaskLowSelectivityBench)->Args({kLongLength, 99});
BENCHMARK(ReplaceWithMaskHighSelectivityBench)->Args({kLongLength, 0});
BENCHMARK(ReplaceWithMaskHighSelectivityBench)->Args({kLongLength, 99});

}  // namespace compute
}  // namespace nebula
